import DebugExt from "../NRLib/base/DebugExt";
import PublicUIBase from "./IPublicUIBase";

const { ccclass, property } = cc._decorator;

export enum TipsStyle_T {
	ST_NONE = 0,		// 默认动画标记
	ST_MOVE = 1 << 0,	// 上浮动画标记
	ST_VOICE = 1 << 1,	// 音效标记
	ST_FTACT = 1 << 2,	// 定制文本动画标识
}

@ccclass
export default class ToastTips extends PublicUIBase {

	// 动画节点
	@property(cc.Node)
	private readonly aniNode: cc.Node = null;

	// tips的文本
	@property(cc.Label)
	private readonly title: cc.Label = null;


	public show(param?: any): void {
		if (!param || !(param.text && param.text.length > 0))
			return;
		super.show(param);
		DebugExt.debug("显示消息：", JSON.stringify(param));
		const type: number = param.data != undefined ? param.data as number : TipsStyle_T.ST_FTACT;
		const timeout: number = param.timeout || 2;
		this.showTips(param.text, type, timeout);
	}

	public clear() {
		this.completeCallback = null;
		this.hide();
	}

	public hide() {
		this.aniNode.stopAllActions();
		if (this.completeCallback)
			this.completeCallback();
		super.hide();
	}

	private completeCallback: () => void
	public setCompleteCallback(callback: () => void) {
		this.completeCallback = callback;
	}

	/**
	 * 飘字提示 -- （音效单独控制；动画标记只有一种有效）
	 * ST_VOICE: 控制是否播放音效；
	 * ST_NONE：节点静止动画标记；
	 * ST_MOVE：节点上移动画标记；
	 * ST_FTACT：定制动画标记；
	 * @param str 字符串内容
	 * @param type TipsStyle_T
	 * @param delayTime [2] tips停滞事件，默认2秒
	 */
	showTips(str: string, type: TipsStyle_T = TipsStyle_T.ST_MOVE | TipsStyle_T.ST_VOICE, delayTime: number = 2) {
		// 判断是否有音效标记
		if ((type & TipsStyle_T.ST_VOICE) == TipsStyle_T.ST_VOICE) {
			if (CC_PREVIEW)
				console.log('show tips with voice~');
		}

		// text 更新文本
		this.title.string = str;
		// animation 处理动画标记
		this.aniNode.opacity = 0;

		if ((type & TipsStyle_T.ST_MOVE) == TipsStyle_T.ST_MOVE) {
			// 节点上移动画标记
			if (CC_PREVIEW)
				console.log('show tips with move~');
			// 初始化节点位置
			this.aniNode.setPosition(cc.v2(0, 0));
			// 开始播放动画
			this.aniNode.runAction(cc.sequence(
				cc.fadeIn(0.1),	// 0.1秒淡入
				cc.moveBy(delayTime, cc.v2(0, 100)),	// Y方向上移100像素
				cc.fadeOut(0.5),	// 淡出0.5秒
				cc.callFunc(this.hide, this)	// 动画结束，清理UI
			));
		}
		else if ((type & TipsStyle_T.ST_FTACT) == TipsStyle_T.ST_FTACT) {
			// 定制动画标记；
			if (CC_PREVIEW)
				console.log('show tips by fangting~');
			// 初始化节点位置
			this.aniNode.setPosition(cc.v2(-300, 100));
			this.aniNode.setScale(1, 0);
			this.aniNode.skewX = -100;
			this.aniNode.color = cc.color(25, 25, 25);
			this.title.node.opacity = 0;
			// 开始播放动画
			this.title.node.runAction(cc.sequence([cc.delayTime(0.2), cc.fadeIn(0.12)]));
			this.aniNode.runAction(cc.sequence(
				cc.spawn([cc.fadeTo(0.20, 225), cc.scaleTo(0.20, 3, 1), cc.skewTo(0.20, 0, 0), cc.moveTo(0.20, 0, 100)]),
				cc.callFunc(() => {
					this.aniNode.width = 1980;
				}),
				cc.scaleTo(0.01, 1, 1),
				cc.delayTime(1.35),
				cc.spawn([cc.fadeOut(0.25), cc.moveTo(0.25, 50, 100), cc.scaleTo(0.25, 1, 0.75)]),
				cc.callFunc(this.hide, this)	// 动画结束，清理UI
			));
		}
		else {
			// 默认动画
			this.aniNode.setPosition(cc.v2(0, -100));
			// 开始播放动画
			this.aniNode.runAction(cc.sequence(
				cc.fadeIn(0.1),	// 0.1秒淡入
				cc.delayTime(delayTime),	// 暂停显示
				cc.callFunc(this.hide, this)	// 动画结束，清理UI
			));
		}
	}
}
